package doublelist;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Map<String,Integer> m = new HashMap<>();
        //添加
        m.put("a",1);
        m.put("b",2);
        m.put("c",3);
        //覆盖
//        m.put("c",4);
        //删除
//        m.remove("a");
//        m.clear();
        boolean flag = m.containsKey("b");
        boolean flag2 = m.containsValue(2);
        System.out.println(m.size());
        System.out.println(flag);
        System.out.println(flag2);
        //使用增强 for遍历
        for(String key:m.keySet()){
            System.out.println(m.get(key));
        }
        //使用lambda表达式遍历
        m.forEach((String key,Integer value)->System.out.println(m.get(key)));
        //使用迭代器遍历
        Iterator<Map.Entry<String, Integer>> it = m.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Integer> entry = it.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
        //创建 set 集合遍历,entrySet
        Set<Map.Entry<String, Integer>> set = m.entrySet();
        for (Map.Entry<String, Integer> entry : set) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

    }
}
